﻿using System;
using System.Collections.Generic;
using System.Text;
using Symbol.Barcode;
using System.Windows.Forms;

namespace Demo02
{
    public class ReaderAction
    {
        public static Reader reader = null;				// 扫描引擎对象
        public static ReaderData readerData = null;		// 扫描引擎读取数据的Buffer
        private static Symbol.Display.Controller _MyDisplayController = null;

        public ReaderAction() { }

        /// <summary>
        /// 初始化Reader.成功返回True,失败返回False.
        /// </summary>
        public static bool InitReader()
        {
            try
            {
                if (reader != null)
                    return false;

                // 创建并激活Reader
                reader = new Reader();
                reader.Actions.Enable();

                // 创建ReaderData
                readerData = new ReaderData(ReaderDataTypes.Text, ReaderDataLengths.MaximumLabel);

                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("初始引擎异常!\n" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 开始Reader的读取
        /// </summary>
        public static void StartRead()
        {
            if (reader != null && readerData != null)
            {
                // 如果有尚未处理完毕的读取,则退出
                if (readerData.IsPending)
                    return;

                try
                {
                    reader.Actions.Read(readerData);	// 开始Reader读取			
                }
                catch (Exception ex)
                {
                    MessageBox.Show("开始引擎读取异常!\n" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 停止Reader的读取
        /// </summary>
        public static void StopRead()
        {
            if (reader != null)
            {
                try
                {
                    reader.Actions.Flush();				// 停止Reader所有读取
                }
                catch (Exception ex)
                {
                    MessageBox.Show("停止引擎读取异常!\n" + ex.Message);
                }
            }
        }

        /// <summary>
        /// 销毁Reader
        /// </summary>
        public static void TermReader()
        {
            try
            {
                if (reader != null)
                {
                    reader.Actions.Disable();
                    reader.Dispose();
                    reader = null;
                }

                if (readerData != null)
                {
                    readerData.Dispose();
                    readerData = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("终止引擎异常!\n" + ex.Message);
            }

        }

        /// <summary>
        /// Stop backlight and disable/close backlight
        /// </summary>
        public static void turnOnBacklight()
        {
            //Select a device from listing
            Symbol.Display.Device MyDevice = (Symbol.Display.Device)Symbol.StandardForms.SelectDevice.Select(
                Symbol.Display.Controller.Title,
                Symbol.Display.Device.AvailableDevices);

            if (MyDevice == null)
            {

                //MessageBox.Show("No Device Selected", "SelectDevice");
                //close the form
                return;
            }

            //Check the device type
            switch (MyDevice.DisplayType)
            {
                // if device is standard device
                case Symbol.Display.DisplayType.StandardDisplay:
                    _MyDisplayController = new Symbol.Display.StandardDisplay(MyDevice);
                    break;

                //if device is simulated device
                case Symbol.Display.DisplayType.SimulatedDisplay:
                    _MyDisplayController = new Symbol.Display.SimulatedDisplay(MyDevice);
                    break;
                default:
                    throw new Symbol.Exceptions.InvalidDataTypeException("Unknown Device Type");

            }

            _MyDisplayController.BacklightState = Symbol.Display.BacklightState.ON;
        }

        /// <summary>
        /// Stop backlight and disable/close backlight
        /// </summary>
        public static void turnOffBacklight()
        {
            _MyDisplayController.BacklightState = Symbol.Display.BacklightState.OFF;
            _MyDisplayController.Dispose();
        }
    }
}
